package com.example.customview.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.example.customview.R

private val text = """Do you remember, the bills you have to pay? I care for no one else but you. The far side of town, where the thin men stalk the streets while the sane stay underground. So I said 'So long', and I waved 'Bye-bye'. It's War-hol, actually. Freak out! They don't dig anything.Sometimes I feel the need to move on; so I pack a bag and move on. Freak out! Last night they loved you, opening doors and pulling some strings. Oh no love, you're not alone. I found my teacher crouching in his overalls. The sun machine is coming down, and we're gonna have a party. The return of the Thin White Duke.Come out of the garden, baby, you'll catch your death in the fog. Sometimes I feel the need to move on; so I pack a bag and move on. It's a moving world, but that's no reason to shoot some of those missiles. Sue, the clinic called; the x-ray's fine. I've never done good things. I care for no one else but you. If you think we're gonna make it, you better hang on to yourself. See these eyes so green? Somewhere, someone's calling me when the chips are down."""
private val BITMAP_SIZE = 150.dp
private val BITMAP_PADDING = 100.dp

class MultilineTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        textSize = 16.dp
    }
    private val bitmap = getAvatar(BITMAP_SIZE.toInt())
    private val fontMetrics = Paint.FontMetrics()

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 绘制图片
        canvas.drawBitmap(bitmap, width - BITMAP_SIZE, BITMAP_PADDING, paint)

        // 绘制文字
        paint.getFontMetrics(fontMetrics)
        val measuredWidth = floatArrayOf(0f)
        var start = 0
        var count: Int
        var maxWidth: Float
        var verticalOffset = -fontMetrics.top
        while (start < text.length) {
            if (verticalOffset + fontMetrics.bottom >= BITMAP_PADDING
                    && verticalOffset + fontMetrics.top <= BITMAP_PADDING + BITMAP_SIZE) {
                maxWidth = width - BITMAP_SIZE
            } else {
                maxWidth = width.toFloat()
            }
            count = paint.breakText(text, start, text.length, true, maxWidth, measuredWidth)
            canvas.drawText(text, start, start + count, 0f, verticalOffset, paint)
            start += count
            verticalOffset += paint.fontSpacing
        }
    }

    private fun getAvatar(width: Int): Bitmap {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeResource(resources, R.drawable.avatar_rengwuxian, options)
        options.inJustDecodeBounds = false
        options.inDensity = options.outWidth
        options.inTargetDensity = width
        return BitmapFactory.decodeResource(resources, R.drawable.avatar_rengwuxian, options)
    }
}